<template>
  <div class="test-container">
    <!-- <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit">
          新增
        </el-button>
        <el-button icon="el-icon-delete" type="danger" @click="handleDelete">
          批量删除
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="12">
        <el-form :inline="true" :model="listQuery" @submit.native.prevent>
          <el-form-item>
            <el-input
              v-model.trim="listQuery.username"
              placeholder="请输入用户名"
              clearable
            />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form> -->
    <div class="top-main">
      <search :listQuery="listQuery" :path="$route.name"></search>
    </div>
    <div class="bottom-main">
      <div class="list-button-group">
        <div class="button-item">
          <el-button type="primary" icon="el-icon-plus" @click="add">
            新增
          </el-button>
        </div>
      </div>
      <div class="table-list">
        <el-table :data="list" :element-loading-text="elementLoadingText" @selection-change="setSelectRows"
          highlight-current-row height="100%" :header-cell-style="{
            'border-bottom': '1px solid #cbcccd',
            'border-top': '1px solid #cbcccd',
          }">
          <el-table-column show-overflow-tooltip type="selection"></el-table-column>
          <el-table-column show-overflow-tooltip prop="daokudh" label="倒库单号"></el-table-column>
          <el-table-column show-overflow-tooltip prop="zhuangtai" label="状态"></el-table-column>
          <el-table-column show-overflow-tooltip prop="chukukf" label="出库库房"></el-table-column>
          <el-table-column show-overflow-tooltip prop="rukukf" label="入库库房"></el-table-column>
          <el-table-column show-overflow-tooltip prop="rukukf" label="物资分类"></el-table-column>
          <el-table-column show-overflow-tooltip prop="rukukf" label="物资区分"></el-table-column>
          <el-table-column show-overflow-tooltip prop="rukukf" label="主管单位"></el-table-column>
          <el-table-column show-overflow-tooltip prop="rukukf" label="创建时间"></el-table-column>
          <el-table-column show-overflow-tooltip label="操作" fixed="right" min-width="100">
            <template #default="{ row }">
              <el-button type="text" @click="handleEdit(row)">明细</el-button>
              <el-button type="text" @click="handleEdit(row)">编辑</el-button>
              <el-button type="text" @click="handleDelete(row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-pagination background :current-page="listQuery.pageIndex" :page-size="listQuery.pageSize" :layout="layout"
        :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange"></el-pagination>
    </div>
    <edit ref="edit" @fetch-data="fetchData"></edit>
    <page-1-add v-if="addshow" :gobacktitle="gobacktitle" :datarow="gobacktitle == '新增' ? {} : editData"></page-1-add>
    <detail v-if="detailshow" :gobacktitle="gobacktitle" :datarow="detailrow"></detail>
  </div>
</template>

<script>
let _listQuery = {
  pageIndex: 1,
  pageSize: 20,
  createTime: [],
  rkdh: '',
  wzmc: '',
  wzqf: '',
  rkzt: '',
  rklx: '',
}
import Page1Add from './components/page1Add.vue'
import Detail from './components/page1Detail.vue'
import search from './components/search.vue'
import { getList, doDelete } from '@/api/userManagement'
import { getDKlist } from '@/api/libraryManagement'
import Edit from './components/TrayManagementEdit'

export default {
  name: 'TrayManagement',
  components: { Edit, search, Page1Add, Detail },
  data() {
    return {
      listQuery: Object.assign({}, _listQuery),
      list: null,
      listLoading: true,
      layout: 'prev, pager, next, jumper',
      total: 0,
      selectRows: '',
      elementLoadingText: '正在加载...',
      listQuery: {
        pageIndex: 1,
        pageSize: 10
      },
      addshow: false,
      detailshow: false,
      gobacktitle: '',
      selectrow: [],
      detailrow: {},
      editData: null
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    setSelectRows(val) {
      this.selectRows = val
    },
    handleEdit(row) {
      if (row.id) {
        this.$refs['edit'].showEdit(row)
      } else {
        this.$refs['edit'].showEdit()
      }
    },
    handleDelete(row) {
      if (row.id) {
        this.$baseConfirm('你确定要删除当前项吗', null, async () => {
          const { msg } = await doDelete({ ids: row.id })
          this.$baseMessage(msg, 'success')
          this.fetchData()
        })
      } else {
        if (this.selectRows.length > 0) {
          const ids = this.selectRows.map((item) => item.id).join()
          this.$baseConfirm('你确定要删除选中项吗', null, async () => {
            const { msg } = await doDelete({ ids })
            this.$baseMessage(msg, 'success')
            this.fetchData()
          })
        } else {
          this.$baseMessage('未选中任何行', 'error')
          return false
        }
      }
    },
    add() {
      this.addshow = true
      this.gobacktitle = '新增'
    },
    async edit(row) {
      const mxData = await queryRukudById({ id: row.id })
      this.addshow = true
      this.gobacktitle = '编辑'
      this.editData = mxData.data
      this.editData.rukudmxList = mxData.data.rukudmx
    },
    handleSizeChange(val) {
      this.listQuery.pageSize = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.listQuery.pageIndex = val
      this.fetchData()
    },
    queryData() {
      this.listQuery.pageIndex = 1
      this.fetchData()
    },
    async fetchData() {
      const { data } = await getDKlist(this.listQuery)
      this.list = data.rows
      this.total = data.total
    },
  },
}
</script>
<style lang="scss" scoped>
.test-container {
  display: flex;
  flex-direction: column;
  padding: 0 !important;
  background: #f6f8f9 !important;
  height: 100%;

  .top-main {
    width: 100%;
    background: #fff;
    padding: 10px 0;
  }

  .bottom-main {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    flex: 1;
    background-color: #fff;
    margin-top: 10px;
    padding-bottom: 10px;

    .list-button-group {
      display: flex;
      flex-direction: row;
      padding: 20px;

      .button-item {
        padding: 0px 10px;
      }

      .button-item:first-child,
      &:last-child {
        padding: 0px 10px 0 0;
      }
    }

    .table-list {
      flex: 1;
      padding: 0 20px 20px;
    }
  }

  .el-button {
    font-size: .14rem !important;
  }
}
</style>
